package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Course;
import com.czh.springboot04.entity.CourseBuy;
import com.czh.springboot04.entity.User;
import com.czh.springboot04.mapper.CourseBuyMapper;
import com.czh.springboot04.mapper.CourseMapper;
import com.czh.springboot04.mapper.UserMapper;
import com.czh.springboot04.service.CourseBuyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseBuyServiceImpl implements CourseBuyService {

    @Autowired
    CourseBuyMapper courseBuyMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public int insertCourseBuy(CourseBuy courseBuy) {
        return courseBuyMapper.insertCourseBuy(courseBuy);
    }
    @Override
    public int updateCourseBuyById(CourseBuy courseBuy) {
        return courseBuyMapper.updateCourseBuyById(courseBuy);
    }
    @Override
    public CourseBuy selectCourseBuyById(Integer buyId) {
        return courseBuyMapper.selectCourseBuyById(buyId);
    }
    @Override
    public PageInfo<CourseBuy> selectCourseBuyAll(Integer courseId,
                                                  Integer memberId,
                                                  double minbuyPrice,
                                                  double maxbuyPrice,
                                                  Integer buyState,
                                                  Integer pageNum,
                                                  Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseBuy> courseBuys = courseBuyMapper.selectCourseBuyAll(courseId, memberId,minbuyPrice,maxbuyPrice,buyState);
        return new PageInfo<>(courseBuys);
    }
    @Override
    public int deleteCourseBuyById(Integer buyId) {
        return courseBuyMapper.deleteCourseBuyById(buyId);
    }

    @Override
    public int deleteCourseBuyByList(List<Integer> buyIdList) {
        return courseBuyMapper.deleteCourseBuyByList(buyIdList);
    }

    @Override
    public PageInfo<CourseBuy> selectCourseBuyList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseBuy> courseBuys = courseBuyMapper.selectCourseBuyList();
        return new PageInfo<>(courseBuys);

    }

    @Override
    public CourseBuy selectCourseBuyByCourseId(int courseId, int memberId) {
        return courseBuyMapper.selectCourseBuyByMemberIdAndCourseId(courseId,memberId);
    }

    @Override
    public PageInfo<CourseBuy> selectByMId(int pageNum, int pageSize,int memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseBuy> courseBuys = courseBuyMapper.selectByMid(memberId);
        for(CourseBuy courseBuy:courseBuys){
            Course course = courseMapper.findCourseByCourseId(courseBuy.getCourseId());
            courseBuy.setCourse(course);
            User user = userMapper.selectById(courseBuy.getMemberId());
            courseBuy.setUser(user);
        }

        PageInfo<CourseBuy> pageInfo = new PageInfo<>(courseBuys);
        return pageInfo;
    }


}
